package com.yanqu.road.server.gameplayer.module.manor;

import com.yanqu.road.entity.config.manor.ManorConfig;
import com.yanqu.road.entity.config.skin.SkinInfo;
import com.yanqu.road.entity.manor.ManorBuildingData;
import com.yanqu.road.logic.bussiness.player.UserManorBusiness;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.skin.SkinModule;
import com.yanqu.road.server.manger.config.SkinMgr;
import com.yanqu.road.server.manger.manor.ManorMgr;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.List;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2021/5/20 18:42
 */
public class ManorBuildingModel {
    private Map<Integer, ManorBuildingData> buildingDataMap;
    private GamePlayer player;

    public ManorBuildingModel(Map<Integer, ManorBuildingData> buildingDataMap, GamePlayer player) {
        this.buildingDataMap = buildingDataMap;
        this.player = player;
        initBuilding();
    }

    private void initBuilding() {
        List<ManorConfig> manorInitBuilding = ManorMgr.getManorInitBuilding();
        for (ManorConfig manorConfig : manorInitBuilding) {
            if (!buildingDataMap.containsKey(manorConfig.getId())) {
                unLock(manorConfig.getId(), true);
            }
        }
    }

    public Map<Integer, ManorBuildingData> getBuildingDataMap() {
        return buildingDataMap;
    }

    public int unLock(int id, boolean isInit) {
        if (buildingDataMap.get(id) != null) {
            return GameErrorCode.E_MANOR_OBJ_IS_UNLOCK;
        }
        //找配置
        ManorConfig manorConfig = ManorMgr.getManorConfigMap().get(id);
        if (manorConfig == null) {
            return GameErrorCode.E_MANOR_CONFIG_NO_FOUND;
        }
        //身份是否达到
        if (player.getUserInfo().getTitleId() < manorConfig.getUnlock()) {
            return GameErrorCode.E_MANOR_TITLE_ID_LIMIT;
        }
        if (!isInit) {
            player.getModule(ManorModule.class).notifyBuildingAdd(id);
        }
        ManorBuildingData buildingData = new ManorBuildingData();
        buildingData.setId(id);
        buildingData.setSkinId(0);
        buildingData.setUserId(player.getUserId());
        buildingData.setInsertOption();
        buildingDataMap.put(id, buildingData);
        //繁荣度奖励
        player.getModule(ManorModule.class).addProsperousValue(Long.valueOf(manorConfig.getPlantRewards()), "建筑解锁");
        if (!isInit) {
            player.getModule(ManorModule.class).syncManorData();
        }
        return 0;
    }

    /**
     * 更换皮肤
     * @param id
     * @param skinId
     * @return
     */
    public int changeSkin(int id, int skinId) {
        ManorBuildingData buildingData = buildingDataMap.get(id);
        if (buildingData == null) {
            return GameErrorCode.E_MANOR_OBJ_IS_LOCK;
        }
        ManorConfig manorConfig = ManorMgr.getManorConfigMap().get(id);
        if (manorConfig == null) {
            return GameErrorCode.E_MANOR_CONFIG_NO_FOUND;
        }
        if (skinId != 0 && player.getModule(SkinModule.class).getUserSkin(skinId) == null) {
            return GameErrorCode.E_MANOR_SKIN_IS_LOCK;
        }
        int type = manorConfig.getType();
        if (skinId != 0) {
            SkinInfo skinInfo = SkinMgr.getSkinInfo(skinId);
            if (skinInfo == null) {
                return GameErrorCode.E_SKIN_NOT_EXIST;
            }
            if (skinInfo.getBelongRole() != type) {
                return GameErrorCode.E_SKIN_NOT_MATCH;
            }
        }
        buildingData.setSkinId(skinId);
        player.getModule(ManorModule.class).syncManorData();
        return 0;
    }


    public void save() {
        for (ManorBuildingData buildingData : buildingDataMap.values()) {
            if (buildingData.isInsertOption()) {
                UserManorBusiness.add(buildingData);
            } else if (buildingData.isUpdateOption()) {
                UserManorBusiness.update(buildingData);
            }
        }
    }
}
